package me.yricky.mayu.ui.fasta_box

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.launch
import me.yricky.mayu.ui.fasta.NewSequenceDialog
import me.yricky.mayu.ui.fasta_list.CountBadge
import me.yricky.mayu.ui.fasta_list.VerticalList
import me.yricky.mayu.ui.fasta.FastaSeqModel

/**
 * @author Yricky
 * @date 2022/4/18
 */

@Composable
fun FastaBoxesModel.Page(modifier: Modifier = Modifier, onSequenceClick:(FastaSeqModel)->Unit = {}){
    Surface(modifier) {

        val drawerState by remember { mutableStateOf(DrawerState(DrawerValue.Closed)) }
        val scope  = rememberCoroutineScope()
        Scaffold(
            scaffoldState = rememberScaffoldState(drawerState),
            topBar = {
                val focusItem by currentFocusedFasta.collectAsState()
                TopAppBar(
                    title = {
                        Row(verticalAlignment = Alignment.CenterVertically){
                            focusItem.let{
                                Text(getFile(it)?.name ?: "临时序列盒", maxLines = 1, modifier = Modifier.weight(1f))
                                it.CountBadge(Modifier.padding(8.dp).wrapContentWidth())
                            }
                        }
                    },
                    modifier = Modifier.fillMaxWidth(),
                    navigationIcon = {
                        Icon(Icons.Rounded.AllInbox,"",Modifier.clickable {
                            scope.launch{ drawerState.open() }
                        }.size(64.dp).padding(12.dp))
                    }
                )

            },
            drawerContent = {
                val fileList by keys.collectAsState()
                LazyColumn {
                    item {
                        Row(Modifier
                            .fillMaxWidth()
                            .background(MaterialTheme.colors.background)
                            .clickable{
                                currentFocusedFasta.value = tempBox
                                scope.launch { drawerState.close() }
                            }.padding(8.dp),
                            verticalAlignment = Alignment.CenterVertically
                        ){
                            Icon(Icons.Rounded.Inbox,"",Modifier.padding(8.dp).size(32.dp))
                            Text("临时序列盒(${tempBox.fastaList.size})", fontSize = 24.sp)
                        }
                    }
                    items(fileList){
                        Row(Modifier
                            .fillMaxWidth()
                            .background(MaterialTheme.colors.background)
                            .clickable{
                                currentFocusedFasta.value = getFasta(it) ?: tempBox
                                scope.launch { drawerState.close() }
                            }.padding(8.dp),
                            verticalAlignment = Alignment.CenterVertically
                        ){
                            Icon(Icons.Rounded.Inbox,"",Modifier.padding(8.dp).size(32.dp))
                            Text(it.name, fontSize = 24.sp)
                            Spacer(Modifier.weight(1f))
                            Icon(
                                Icons.Rounded.Close,"",
                                Modifier
                                    .clickable { remove(it) }
                                    .size(32.dp)
                            )
                        }
                    }
                }
            },
            content = {
                val focusItem by currentFocusedFasta.collectAsState()
                focusItem.VerticalList(onClick = onSequenceClick)
            },
            floatingActionButton = {
                val focusItem by currentFocusedFasta.collectAsState()
                if(focusItem == tempBox){
                    var showDialog by remember { mutableStateOf(false) }
                    FloatingActionButton({showDialog = true}){
                        Icon(Icons.Rounded.Add,"")
                        if(showDialog){
                            NewSequenceDialog({showDialog = false}){
                                tempBox.fastaList.add(0,it)
                            }
                        }
                    }
                }
            }
        )
    }
}